package com.lanyun.aicorrectassistant.domain.authUser.impl;

import com.lanyun.aicorrectassistant.basic.entity.AuthUser;
import com.lanyun.aicorrectassistant.basic.service.AuthUserService;
import com.lanyun.aicorrectassistant.constant.JwtClaimsConstant;
import com.lanyun.aicorrectassistant.controller.admin.authUser.vo.AuthUserLoginResponseVO;
import com.lanyun.aicorrectassistant.controller.admin.authUser.vo.AuthUserRequestPageVO;
import com.lanyun.aicorrectassistant.controller.admin.authUser.vo.AuthUserRequestVO;
import com.lanyun.aicorrectassistant.controller.admin.authUser.vo.AuthUserResponseVO;
import com.lanyun.aicorrectassistant.convert.AuthUserConvert;
import com.lanyun.aicorrectassistant.domain.authUser.AuthUserDomainService;
import com.lanyun.aicorrectassistant.basic.bean.PageResult;
import com.lanyun.aicorrectassistant.enums.IsDeletedFlagEnum;
import com.lanyun.aicorrectassistant.properties.JwtProperties;
import com.lanyun.aicorrectassistant.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 用户表领域服务实现
 *
 * @author makejava
 * @version 1.0
 * @date 2025-05-12 01:53:18
 */
@Service
public class AuthUserDomainServiceImpl implements AuthUserDomainService {

    @Resource
    private AuthUserService authUserService;

    @Resource
    private JwtProperties jwtProperties;

    @Override
    public PageResult<AuthUserResponseVO> page(AuthUserRequestPageVO requestPageVO) {
        PageResult<AuthUserResponseVO> pageResult = new PageResult<>();
        AuthUser queryEntity = AuthUserConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<AuthUser> entities = authUserService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = authUserService.count(queryEntity);
        List<AuthUserResponseVO> vos = AuthUserConvert.INSTANCE.convertEntityToRespList(entities);

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(AuthUserRequestVO requestVO) {
        AuthUser entity = AuthUserConvert.INSTANCE.convert(requestVO);
        return authUserService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        AuthUser entity = new AuthUser();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return authUserService.update(entity) != 0;
    }

    @Override
    public Boolean insert(AuthUserRequestVO requestVO) {
        AuthUser entity = AuthUserConvert.INSTANCE.convert(requestVO);
        entity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
        return authUserService.insert(entity) != 0;
    }

    @Override
    public AuthUserResponseVO get(Long id) {
        AuthUser entity = authUserService.queryById(id);
        return AuthUserConvert.INSTANCE.convert(entity);
    }

    @Override
    public AuthUserLoginResponseVO doLogin(AuthUserRequestVO authUserRequestVO) {
        AuthUser authUserConvert = AuthUserConvert.INSTANCE.convert(authUserRequestVO);
        authUserConvert.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
        AuthUser authUser = authUserService.queryByCondition(authUserConvert);
        if (authUser == null) {
            throw new RuntimeException("用户名或密码错误");
        }

        HashMap<String, Object> clalms = new HashMap<>();
        clalms.put(JwtClaimsConstant.USER_ID, authUser.getId());
        clalms.put(JwtClaimsConstant.USER_ROLE, authUser.getRole());

        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), clalms);

        return AuthUserLoginResponseVO.builder()
                .id(authUser.getId())
                .realName(authUser.getRealName())
                .userName(authUser.getUsername())
                .token(token)
                .role(authUser.getRole())
                .build();
    }
}
